package net.zjitc.controller;

import net.zjitc.pojp.CateGory;
import net.zjitc.pojp.CateGorySecond;
import net.zjitc.service.AdminCateGorySecondService;
import net.zjitc.service.AdminCateGoryService;
import net.zjitc.service.impl.AdminCateGorySecondServiceImpl;
import net.zjitc.service.impl.AdminCateGoryServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by Administrator on 2017/11/15.
 */
public class AdminCateGorySecond {
    AdminCateGorySecondService adminCateGorySecondService=new AdminCateGorySecondServiceImpl();
    AdminCateGoryService adminCateGoryService=new AdminCateGoryServiceImpl();
    //显示列表
    @URLMapping(url="/AdminCategorySecondFindAll")
    public String findAll(HttpServletRequest request, HttpServletResponse response){
        List<CateGorySecond> list=adminCateGorySecondService.findAll();
        request.setAttribute("list",list);
        return "/admin/categorysecond/twolist.jsp";
    }
    //显示添加页面
    @URLMapping(url = "/AdminCategorySecondAddUI")
    public String addUI(HttpServletRequest request, HttpServletResponse response){
        //准备一级分类的回显数据
        List<CateGory> clist=adminCateGoryService.findOneList();
        request.setAttribute("clist",clist);

        return "/admin/categorysecond/add.jsp";
    }

    //添加页面
    @URLMapping(url = "/AdminCategorySecondAdd")
    public String add(HttpServletRequest request, HttpServletResponse response){
       //csname cid
        CateGory cateGory=new CateGory();
        CateGorySecond cateGorySecond=new CateGorySecond();
        try {
            BeanUtils.populate(cateGory,request.getParameterMap());
            BeanUtils.populate(cateGorySecond,request.getParameterMap());
            cateGorySecond.setCateGory(cateGory);
            int count=adminCateGorySecondService.insert(cateGorySecond);
        } catch (Exception e) {
            e.printStackTrace();
        }


        //添加完后。要重定向到...
        String url=request.getContextPath()+"/AdminCategorySecondFindAll";
        return "Redirect:"+url;

    }

    //显示修改页面
    @URLMapping(url = "/AdminCategorySecondEditUI")
    public String editUI(HttpServletRequest request, HttpServletResponse response){
        String csid=request.getParameter("csid");
        CateGorySecond cateGorySecond=adminCateGorySecondService.findByCSid(csid);
        request.setAttribute("cs",cateGorySecond);
        List<CateGory> list=adminCateGoryService.findOneList();
         request.setAttribute("clist",list);
        return "/admin/categorysecond/editUI.jsp";
    }

    //更新功能
    @URLMapping(url = "/AdminCategorySecondEdit")
     public String edit(HttpServletRequest request, HttpServletResponse response){
      CateGory cateGory=new CateGory();
      CateGorySecond cateGorySecond=new CateGorySecond();
        try {
            BeanUtils.populate(cateGory,request.getParameterMap());
            BeanUtils.populate(cateGorySecond,request.getParameterMap());
            cateGorySecond.setCateGory(cateGory);
            int count=adminCateGorySecondService.update(cateGorySecond);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Redirect:"+request.getContextPath()+"/AdminCategorySecondFindAll";
    }



    //删除功能
    @URLMapping(url = "/AdminCategorySecondDelete")
    public String delete(HttpServletRequest request,HttpServletResponse response){

        return "Redirect:"+request.getContextPath()+"/AdminCategorySecondFindAll";
    }

}
